﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using Classes;
using System.Data;
using System.Drawing;
using System.Text;

namespace AvaliacaoWeb
{
    public partial class Avaliar : System.Web.UI.Page
    {
        private String DecodeFromUtf8(String utf8_String)
        {            
            byte[] bytes = Encoding.Default.GetBytes(utf8_String);
            return Encoding.UTF8.GetString(bytes);            
        }

        DataTable dtResp = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            btnGravar.Attributes.Add("onclick", "document.getElementById('imgCarregando').style.display='block';"); 
            btnVoltar.Attributes.Add("onclick", "document.getElementById('imgCarregando').style.display='block';"); 

            if (!IsPostBack)
            {
                // carregando as respostas
                String sqlResp = " Select cod, des From a_conven where numava = " + Request.QueryString["numava"].ToString();

                QueryCommand qryCommandResp = new QueryCommand(sqlResp, "Default");
                dtResp = DataService.GetDataSet(qryCommandResp).Tables[0];



                AIdava ava = new AIdava(Request.QueryString["numava"].ToString());
                lblTitulo.Text = ava.Titulo;
                lblAvaliando.Text = "Avaliando (" + Request.QueryString["avaliado"].ToString() + ")";
                String sql = " Select * From a_quest where numava = " + Request.QueryString["numava"].ToString() +
                            " order by numqts";

                QueryCommand qryCommand = new QueryCommand(sql, "Default");

                DataTable dt = new DataTable();
                dt = DataService.GetDataSet(qryCommand).Tables[0];

                dtGrid.DataSource = dt;
                dtGrid.DataBind();
            }
        }

        protected void dtGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label questao = e.Row.FindControl("questao") as Label;
                TextBox codcon = e.Row.FindControl("codcon") as TextBox;
                RadioButtonList opcoes = e.Row.FindControl("radOpcoes") as RadioButtonList;


                if (codcon.Text.Trim() == "")
                {
                    opcoes.Visible = false;
                    questao.Font.Bold = true;
                    questao.Font.Size = 15;
                    e.Row.BackColor = Color.FromArgb(79, 79, 79);
                    questao.ForeColor = Color.White;
                }
                else
                {
                    int tamanho = codcon.Text.Length;
                    for (int i = 1; i <= tamanho; i++)
                    {
                        DataRow[] item = dtResp.Select("cod = " + codcon.Text.Substring(i-1,1));
                        opcoes.Items.Add(new ListItem(item[0]["des"].ToString(), item[0]["cod"].ToString()));
                    }
                }


                //txtNota.ID = "nota_" + (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            AIdava ava = new AIdava(Request.QueryString["numava"].ToString());

            if (ava.Tipava == 8)
                Response.Redirect("Lista.aspx?numava="+ava.Numava.ToString());
        }

        protected void btnGravar_Click(object sender, EventArgs e)
        {
            lblMsgErro.Visible = false;
            int qtdNaoResp = 0;

            foreach (GridViewRow linha in dtGrid.Rows)
            {
                TextBox codcon = linha.FindControl("codcon") as TextBox;

                if (codcon.Text.Trim() != "")
                {
                    TextBox id = linha.FindControl("lblId") as TextBox;
                    RadioButtonList opcoes = linha.FindControl("radOpcoes") as RadioButtonList;

                    if (opcoes.SelectedValue == "")
                    {
                        qtdNaoResp++;
                        linha.BackColor = Color.FromArgb(242, 222, 223);
                    }
                    else
                    {
                        linha.BackColor = Color.FromArgb(245, 245, 245);                        
                    }
                }
            }

            if (qtdNaoResp > 0)
            {
                lblMsgErro.Visible = true;
            }
            else
            {
                foreach (GridViewRow linha in dtGrid.Rows)
                {
                    TextBox codcon = linha.FindControl("codcon") as TextBox;

                    if (codcon.Text.Trim() != "")
                    {
                        TextBox id = linha.FindControl("lblId") as TextBox;
                        RadioButtonList opcoes = linha.FindControl("radOpcoes") as RadioButtonList;

                        //resposta.Items.Add("Questão " + id.Text + ", R=" + opcoes.SelectedValue + "\n");

                        String sql = "Update a_avaliacao set resp = '" + opcoes.SelectedValue + "'" +
                                     " Where numava = " + Request.QueryString["numava"].ToString() + 
                                     " And numqts = " + id.Text +
                                     " And avaliado = '" + DecodeFromUtf8(Request.QueryString["avaliado"].ToString().Trim()) + "'" +
                                     " And matric = " + Session["Matric"].ToString();

                        QueryCommand qry = new QueryCommand(sql, "Default");
                        DataService.ExecuteQuery(qry);

                    }
                }

                AIdava ava = new AIdava(Request.QueryString["numava"].ToString());
                Response.Redirect("Lista.aspx?numava=" + ava.Numava.ToString());
            }

            
        }
    }
}